﻿using EventHandlerDemo.Domain.Orders;
using EventHandlerDemo.Infrastructure.EFCore;

namespace EventHandlerDemo.Application.Orders;

public interface IOrderAppService
{
    Task<int> CreateaAsync();
}

public class OrderAppService : IOrderAppService
{
    private readonly AppDbContext _dbContext;

    public OrderAppService(AppDbContext dbContext)
    {
        _dbContext = dbContext;
    }

    public async Task<int> CreateaAsync()
    {
        using (_dbContext.Database.BeginTransaction())
        {
            var order = new Order()
            {
                Name = Guid.NewGuid().ToString(),
            };
            await _dbContext.Orders.AddAsync(order);
            await _dbContext.SaveChangesAsync();
            return order.Id;
        }
    }
}